package com.honry.identityaccess.domain.model.identity;

import java.io.Serializable;
import java.util.regex.Pattern;

import com.honry.common.AssertionConcern;

public final class EmailAddress extends AssertionConcern implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String address;
	protected EmailAddress() {
        super();
    }
	public EmailAddress(String address) {
		this.setAddress(address);
	}
	public EmailAddress(EmailAddress emailAddress) {
		this(emailAddress.address);
	}
	public String address(){
		return this.address;
	}
	private void setAddress(String address){
		this.assertArgumentNotEmpty(address, "必填项.");
        this.assertArgumentLength(address, 1, 100, "长度必须小于100.");
        this.assertArgumentTrue(
                Pattern.matches("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", address),
                "email地址格式错误.");
		this.address=address;
	}
	@Override
	public boolean equals(Object object){
		boolean equlasObjects=false;
		if(object!=null&&object.getClass()==this.getClass())
		{
			EmailAddress typeObject=(EmailAddress)object;
			
			equlasObjects=typeObject.address.equals(this.address());
		}
		return equlasObjects;
	}
	@Override
	public int hashCode(){
		int hashCodeValue = (17861 * 179)+ this.address().hashCode();

	        return hashCodeValue;
	}
	@Override
	public String toString(){
		return "EmailAddress[address="+this.address+"]";
	}

}
